// pages/new/home/index.js
const http = require("../../../utils/http.js"); //引用
const app = getApp()
Component({
  properties: {

  },

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    WindowHeight: app.globalData.WindowHeight,
    ScreenHeight: app.globalData.ScreenHeight,
    mddList: [],
    queryParam: {
      pageIndex: 1,
      pageSize: 15,
      searchName: ''
    },
    animationData: {},
    topNum: 0,
    floorstatus: false,

    inputShowed: false,
    inputVal: "",
  },

  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.setData({
        search: this.search.bind(this)
      })
      this.getData(this.data.queryParam);
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行

    }
  },

  methods: {
    getData(params) {
      this.setData({
        triggered: true,
      })
      http.get("/mdd/getMdd", params).then(res => {
        if (res.data.status == 200) {
          let data = []
          data.push(res.data.data.resultList)
          this.setData({
            mddList: data
          })
        } else {
          this.setData({
            mddList: []
          })
        }
        this.setData({
          triggered: false,
        })
      })
    },
    addData(params) {
      params.pageIndex = params.pageIndex + 1
      this.setData({
        queryParam: params
      })
      http.get("/mdd/getMdd", params).then(res => {
        if (res.data.status == 200) {
          let data = this.data.mddList
          data.push(res.data.data.resultList)
          this.setData({
            mddList: data
          })
        } else {
          params.pageIndex = params.pageIndex - 1
          this.setData({
            queryParam: params
          })
          return;
        }
      })
    },
    //刷新
    refresh: function(e) {
      this.setData({
        topNum: this.data.topNum = 0
      });
      let id = setInterval(() => {
        app.rotateAni(this, 'animationData', 'num')
      }, 500)
      setTimeout(() => {
        this.onRefresh()
        clearInterval(id);
      }, 500)

    },
    //回到顶部
    goTop: function(e) { // 一键回到顶部
      this.setData({
        topNum: this.data.topNum = 0
      });
    },
    onRefresh() {
      let params = {
        pageIndex: 1,
        pageSize: 15
      }
      this.setData({
        queryParam: params
      })
      this.getData(params)
    },
    toLowFun(e) {
      this.addData(this.data.queryParam);
    },
    // 获取滚动条当前位置
    scrolltoupper: function(e) {
      if (e.detail.scrollTop > 100) {
        this.setData({
          floorstatus: true
        });
      } else {
        this.setData({
          floorstatus: false
        });
      }
    },
    search(e) {
      let params = {
        pageIndex: 1,
        pageSize: 15,
        searchName: e.detail.value
      }
      this.setData({
        queryParam: params
      })
      this.getData(this.data.queryParam)
    },
    clear(e){
      let params = {
        pageIndex: 1,
        pageSize: 15,
        searchName: ''
      }
      this.setData({
        queryParam: params
      })
      this.getData(this.data.queryParam)
    },
    chooseMdd(e){
      wx.navigateTo({
        url: "/pages/destination/views/detail/detail?mddId=" + e.currentTarget.dataset.item
      })
    }
  }
})